package com.zc.portal.iface.service;

import com.zc.portal.domain.dto.SysUrlPattenAttribute;
import com.zc.portal.iface.repository.UrlPattenAttributeRepositroy;
import com.zc.portal.infra.security.metadata.UrlInvocationSecurityMetadataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Service
public class RoleResourceService {

    private static final Log log = LogFactory.getLog(RoleResourceService.class);

    @Autowired
    private UrlInvocationSecurityMetadataSource urlInvocationSecurityMetadataSource;

    @Autowired
    private UrlPattenAttributeRepositroy urlPattenAttributeRepositroy;

    public void refreshUrlAttributes() {
        urlInvocationSecurityMetadataSource.reloadRolesAndUrls();
        log.info("路由权限刷新完成！");
    }

    /**
     * 设置url访问角色
     */
    public void addUrlPattenToRole(@NotBlank String urlPatten, @NotNull Integer roleId, String editor, Date editTime) {
        SysUrlPattenAttribute sysUrlPattenAttribute = new SysUrlPattenAttribute();
        sysUrlPattenAttribute.setUrlPatten(urlPatten);
        sysUrlPattenAttribute.setEditor(editor);
        sysUrlPattenAttribute.setEditTime(editTime);
        sysUrlPattenAttribute.setRoleId(roleId);
        sysUrlPattenAttribute.setEnabled("1");
        this.addUrlPattenToRole(sysUrlPattenAttribute);
    }

    public void addUrlPattenToRole(SysUrlPattenAttribute urlPattenAttribute) {
        urlPattenAttributeRepositroy.addUrlConfigAttribute(urlPattenAttribute);
    }


}
